﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace CommonLibraries.DeviceLibrary
{
    public class NmeaGGA : CommonLibraries.DeviceLibrary.INmeaGGA
    {
        public NmeaGGA(string sentence)
        {
            try
            {
                string finalSentence;
                if (NmeaUtility.IsValidSentence(sentence))
                {
                    finalSentence = NmeaUtility.LastSentence(sentence);
                    this.Words = new List<string>(finalSentence.Split(','));
                    if (this.Words.Count >= 13 && NmeaUtility.ConvertToInt(Words[6]) > 0 && NmeaUtility.IsValidSentence(finalSentence))
                    {
                        this.Latitude = Words[2];
                        this.LatitudeHemisphere = Words[3];
                        this.Longitude = Words[4];
                        this.LongitudeHemisphere = Words[5];
                        this.Quality = NmeaUtility.ConvertToInt(Words[6]);
                        this.SateliteCount = NmeaUtility.ConvertToInt(Words[7]);
                        this.HorizontalDilution = NmeaUtility.ConvertToDouble(Words[8]);
                        this.Altitude = NmeaUtility.ConvertToDouble(Words[9]);
                        this.AltitudeUnit = Words[10];
                        this.GeoidHeight = NmeaUtility.ConvertToDouble(Words[11]);
                        this.GeoidHeightUnit = Words[12];
                    }
                    this.SateliteTime = NmeaUtility.ConvertSateliteTime(Words[1]);
                }
                //if (_arrTempSentence.Length >= 3)
                //{
                //    string tempSentence = _arrTempSentence[_arrTempSentence.Length - 3];
                //}
            }
            catch (Exception e)
            {

                CreateDebug("NmeaGGA->Sentence" + e.Message);
            }

            
        }


        private bool isCorrectStreamSentence(string _StreamSentece)
        {
            try
            {
                string[] _tempSentence;
                if (!_StreamSentece.StartsWith("$GPGGA")) return false;
                _tempSentence = _StreamSentece.Split(',');
                if (_tempSentence.Length < 13) return false;

                //if satellites < 3 or latitude = 0 or Longitude = 0 or altitude = 0
                if (NmeaUtility.ConvertToInt(_tempSentence[6]) == 0 || String.IsNullOrEmpty(_tempSentence[2]) || String.IsNullOrEmpty(_tempSentence[4]) || String.IsNullOrEmpty(_tempSentence[9]) || NmeaUtility.ConvertToInt(_tempSentence[7]) < 3)
                    return false;
                return true;


            }
            catch (Exception ex)
            {
                CreateDebug("NmeaGGA->isCorrectStreamSentence -->\r\n" + ex.Message + "\r\n" + ex.StackTrace);
                return false;
            }
        }

        private DateTime prop_SateliteTime = DateTime.MinValue;
        public DateTime SateliteTime
        {
            get
            {
                return prop_SateliteTime;
            }
            set
            {
                prop_SateliteTime = value;
            }
        }
        private string prop_Latitude = "0";
        public string Latitude
        {
            get
            {
                return prop_Latitude;
            }
            set
            {
                prop_Latitude = value;
            }
        }
        private string prop_LatitudeHemisphere;
        public string LatitudeHemisphere
        {
            get
            {
                return prop_LatitudeHemisphere;
            }
            set
            {
                prop_LatitudeHemisphere = value;
            }
        }
        private string prop_Longitude = "0";
        public string Longitude
        {
            get
            {
                return prop_Longitude;
            }
            set
            {
                prop_Longitude = value;
            }
        }
        private string prop_LongitudeHemisphere;
        public string LongitudeHemisphere
        {
            get
            {
                return prop_LongitudeHemisphere;
            }
            set
            {
                prop_LongitudeHemisphere = value;
            }
        }
        private int prop_Quality;
         /// <summary>
        /// Fix quality: 
        ///     0 = invalid
        ///     1 = GPS fix (SPS)
        ///     2 = DGPS fix
        ///     3 = PPS fix
        ///     4 = Real Time Kinematic
        ///     5 = Float RTK
        ///     6 = estimated (dead reckoning) (2.3 feature)
        ///     7 = Manual input mode
        ///     8 = Simulation mode
        /// </summary>
        public int Quality
        {
            get
            {
                return prop_Quality;
            }
            set
            {
                prop_Quality = value;
            }
        }
        private int prop_SateliteCount;
        public int SateliteCount
        {
            get
            {
                return prop_SateliteCount;
            }
            set
            {
                prop_SateliteCount = value;
            }
        }
        private double prop_HorizontalDilution;
        public double HorizontalDilution
        {
            get
            {
                return prop_HorizontalDilution;
            }
            set
            {
                prop_HorizontalDilution = value;
            }
        }
        private double prop_Altitude;
        public double Altitude
        {
            get
            {
                return prop_Altitude;
            }
            set
            {
                prop_Altitude = value;
            }
        }
        private string prop_AltitudeUnit;
        public string AltitudeUnit
        {
            get
            {
                return prop_AltitudeUnit;
            }
            set
            {
                prop_AltitudeUnit = value;
            }
        }
        private double prop_GeoidHeight;
        public double GeoidHeight
        {
            get
            {
                return prop_GeoidHeight;
            }
            set
            {
                prop_GeoidHeight = value;
            }
        }
        private string prop_GeoidHeightUnit;
        public string GeoidHeightUnit
        {
            get
            {
                return prop_GeoidHeightUnit;
            }
            set
            {
                prop_GeoidHeightUnit = value;
            }
        }
        private string prop_Checksum;
        public string Checksum
        {
            get
            {
                return prop_Checksum;
            }
            set
            {
                prop_Checksum = value;
            }
        }
        private string prop_Sentence;
        public string Sentence
        {
            get
            {
                return prop_Sentence;
            }
            set
            {
                prop_Sentence = value;
            }
        }
        private List<string> prop_Words;
        public List<string> Words
        {
            get
            {
                return prop_Words;
            }
            set
            {
                prop_Words = value;
            }
        }

        public bool HasSolution
        {
            get { return this.Quality > 0; }
        }


        protected void CreateTxt(string debugData)
        {
            StreamWriter _a = File.AppendText(".\\DebugGMS.txt");
            _a.WriteLine("##############################");
            _a.WriteLine(System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToShortTimeString());
            _a.WriteLine("##############################");
            _a.WriteLine(debugData);
            _a.Close();
            _a.Dispose();
        
        }

        protected void CreateDebug(string debugData)
        {
            StreamWriter _a = File.AppendText(".\\DebugClasses.txt");
            _a.WriteLine("##############################");
            _a.WriteLine(System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToShortTimeString());
            _a.WriteLine("##############################");
            _a.WriteLine(debugData);
            _a.Close();
            _a.Dispose();

        }

    }
}
